﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UIManager : MonoBehaviour
{
    private static UIManager instance;

    public float hurtSpeed = 0.001f;

    public Image HealthPointImg;
    public Image HealthEffectImg;

    private float remainingHP;
    private float maxHP;

    private void Awake()
    {
        if (instance != null)
        {
            Destroy(gameObject);
            return;
        }
        instance = this;

        DontDestroyOnLoad(gameObject);
    }

    public static UIManager Instance
    {
        get => instance;
    }


    public void PlayerHurt(float hurtHP, float remainingHP, float maxHP)
    {
        Debug.Log($"PlayerHurt hurt:{hurtHP}, HP:{remainingHP}/{maxHP}");

        this.remainingHP = remainingHP;
        this.maxHP = maxHP;

        HealthPointImg.fillAmount = remainingHP / maxHP;

        StartCoroutine("PlayerHurtCoroutine");
    }

    // 使用携程来更新底色
    private IEnumerator PlayerHurtCoroutine()
    {
        while (HealthEffectImg.fillAmount > HealthPointImg.fillAmount)
        {
            HealthEffectImg.fillAmount -= hurtSpeed;
            yield return new WaitForSeconds(0.005f);
        }
        if (HealthEffectImg.fillAmount <= HealthPointImg.fillAmount)
        {
            HealthEffectImg.fillAmount = HealthPointImg.fillAmount;
        }
    }

}
